<?php

require_once './config.php';

$gid = $_REQUEST['gid'];
$id = $_REQUEST['id'];
$price = $_REQUEST['price'];
$img = $_REQUEST['img'];
$gname = $_REQUEST['gname'];
$shumu = $_REQUEST['shumu'];

$sql = "SELECT * FROM jiagou WHERE gid = $gid AND id = '$id'";

$result = mysqli_query($conn,$sql);

$data = mysqli_fetch_assoc($result);

if($data){
    $shumu = $data['shumu'] + $shumu;
    $xuhao = $data['xuhao'];

    $sql = "UPDATE jiagou SET shumu = $shumu WHERE xuhao = $xuhao";

    mysqli_query($conn,$sql);

    $rows = mysqli_affected_rows($conn);
}else{
    $sql = "INSERT INTO jiagou (gid, id, price, img, gname, shumu) VALUES ($gid, '$id', $price, '$img', '$gname', $shumu)";

    mysqli_query($conn, $sql);

    $rows = mysqli_affected_rows($conn);
}

if($rows > 0){
    echo json_encode(array(
        'code' => 1,
        'msg' => '加购成功'
      ));
}else{
    echo json_encode(array(
        'msg' => '失败'
    ));
}

